﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using MiniJSON;
using UnityEngine.UI;

public class LayersLoader : MonoBehaviour
{
    public TextAsset psdJson;
    public Transform psdParent;

    public void BuildLayers()
    {
#if UNITY_EDITOR
        print("buildingLayers...");

        string path = AssetDatabase.GetAssetPath(psdJson);
        DirectoryInfo directoryInfo = new DirectoryInfo(path);
        string parentPath = directoryInfo.Parent.ToString();
        LoadTemplatePsdJson(psdJson.text, out float basePixelWidth, out float basePixelHeight, out Dictionary<string, Rect> rectDictionary);

        foreach (var key in rectDictionary.Keys)
        {
            string loadPath = Path.Combine(parentPath, key);

            string assetsPath = loadPath.Split(new string[] { "Assets" }, System.StringSplitOptions.None)[1];
            //assetsPath = Path.Combine("Assets", assetsPath);
            assetsPath = "Assets" + assetsPath;
            Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(assetsPath + ".png");
            if (sprite)
            {
                GameObject gameObject = new GameObject(sprite.name, typeof(Image));
                gameObject.transform.SetParent(psdParent);
                gameObject.transform.SetAsFirstSibling();
                gameObject.GetComponent<Image>().sprite = sprite;

                RectTransform puzzleElementRT = gameObject.transform as RectTransform;

                //RectTransform canvasRT = psdParent.root.GetComponent<RectTransform>();

                //puzzleElementRT.anchoredPosition = transform.root.localScale.x* new Vector2(rectDictionary[key].x, rectDictionary[key].y);
                puzzleElementRT.localPosition = new Vector2(rectDictionary[key].x, rectDictionary[key].y);

                //设置元素图片的大小
                //puzzleElementRT.sizeDelta = transform.root.localScale.x * new Vector2(rectDictionary[key].width, rectDictionary[key].height);
                puzzleElementRT.sizeDelta = psdParent.root.localScale.x * new Vector2(rectDictionary[key].width, rectDictionary[key].height);


                ////创建该元素图片的特征点

                //allPuzzleElementDictionary[pngName].CreatAllFeaturePoints();
            }
        }
#endif
    }

    private void LoadTemplatePsdJson(string psdJsonText,
                                     out float basePixelWidth,
                                     out float basePixelHeight,
                                     out Dictionary<string, Rect> rectDictionary)
    {
        //基础宽高初始化
        basePixelWidth = 1440;
        basePixelHeight = 2800;

        rectDictionary = new Dictionary<string, Rect>();


        //解析为字典
        Dictionary<string, object> psdInfoDictionary = Json.Deserialize(psdJsonText) as Dictionary<string, object>;

        //进一步解析出“skins”字典
        Dictionary<string, object> skinsInfoDictionary = psdInfoDictionary["skins"] as Dictionary<string, object>;

        //进一步解析出“default”字典
        Dictionary<string, object> defaultInfoDictionary = skinsInfoDictionary["default"] as Dictionary<string, object>;

        // 遍历“default”字典的所有key
        foreach (string defaultKey in defaultInfoDictionary.Keys)
        {
            //进一步解析出“sprite”字典
            Dictionary<string, object> spriteInfoDictionary = defaultInfoDictionary[defaultKey] as Dictionary<string, object>;

            //遍历“sprite”字典的所有key
            foreach (string spriteKey in spriteInfoDictionary.Keys)
            {
                //print(spriteKey);
                if (!spriteKey.Contains("child"))
                {
                    //进一步解析出“png”字典
                    Dictionary<string, object> pngInfoDictionary = spriteInfoDictionary[spriteKey] as Dictionary<string, object>;

                    //记录某个png的像素Rect信息
                    Rect pngPixelRect = new Rect
                    {
                        x = float.Parse(pngInfoDictionary["x"].ToString()),
                        y = float.Parse(pngInfoDictionary["y"].ToString()),
                        width = float.Parse(pngInfoDictionary["width"].ToString()),
                        height = float.Parse(pngInfoDictionary["height"].ToString())
                    };

                    //将该png的像素Rect信息加入到对应的字典中
                    rectDictionary.Add(spriteKey, pngPixelRect);
                }
            }
        }
    }
}
